
package com.cptravel.activity.explore;

import java.util.ArrayList;
import java.util.List;

import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemSelectedListener;
import android.widget.ArrayAdapter;
import android.widget.EditText;
import android.widget.Spinner;

import com.core.api.ApiListenrDefaultImpl;
import com.core.api.B5MBaseResponse;
import com.core.controller.B5MBaseActivity;
import com.core.util.B5MActivityHelper;
import com.core.util.B5MToaster;
import com.cptravel.R;
import com.cptravel.api.GetproducttypeRequest;
import com.cptravel.api.GetproducttypeResponse;
import com.cptravel.api.GetserveltclassRequest;
import com.cptravel.api.GetserveltclassResponse;
import com.cptravel.bean.Class;
import com.cptravel.bean.ProductType;

public class SearchAdvancedActivity extends B5MBaseActivity
{
    private GetserveltclassRequest mGetserveltclassRequest;
    private Spinner mSpinnerClass, mSpinnerType;
    private ArrayAdapter<String> mArrayAdapterClass, mArrayAdapterType;
    private EditText et_area, et_keyword, et_lowestprice, et_highestprice;
    private List<String> mListClass, mListType;
    private GetproducttypeRequest mGetproducttypeRequest;
    private String typeid;
    protected GetproducttypeResponse getproducttypeResponse;

    @Override
    protected String getHeadTitle()
    {
        // TODO Auto-generated method stub
        return "高级搜索";
    }

    @Override
    public int doGetContentViewId()
    {
        // TODO Auto-generated method stub
        return R.layout.search_advanced;
    }

    @Override
    public void doInitSubViews(View containerView)
    {
        // TODO Auto-generated method stub
        mSpinnerClass = (Spinner) findViewById(R.id.spinnerClass);
        et_area = (EditText) findViewById(R.id.et_area);
        et_keyword = (EditText) findViewById(R.id.et_keyword);
        et_lowestprice = (EditText) findViewById(R.id.et_lowestprice);
        et_highestprice = (EditText) findViewById(R.id.et_highestprice);
        mSpinnerType = (Spinner) findViewById(R.id.spinnerType);
        mSpinnerType.setOnItemSelectedListener(new OnItemSelectedListener()
        {
            @Override
            public void onItemSelected(AdapterView<?> parent , View view , int position , long id)
            {
                // TODO Auto-generated method stub
                typeid = getproducttypeResponse.productTypes.get((int) id).typeid;
            }

            @Override
            public void onNothingSelected(AdapterView<?> parent)
            {
                // TODO Auto-generated method stub
            }
        });
    }

    @Override
    public void doInitDataes()
    {
        // TODO Auto-generated method stub
        mListClass = new ArrayList<String>();
        mListType = new ArrayList<String>();
        mArrayAdapterClass = new ArrayAdapter<String>(mContext, R.layout.search_spinner_item, mListClass);
        mArrayAdapterType = new ArrayAdapter<String>(mContext, R.layout.search_spinner_item, mListType);
        mSpinnerClass.setAdapter(mArrayAdapterClass);
        mSpinnerType.setAdapter(mArrayAdapterType);
        mGetserveltclassRequest = new GetserveltclassRequest(new ApiListenrDefaultImpl()
        {
            @Override
            public void onSuccess(B5MBaseResponse b5mBaseResponse)
            {
                // TODO Auto-generated method stub
                super.onSuccess(b5mBaseResponse);
                GetserveltclassResponse getserveltclassResponse = (GetserveltclassResponse) b5mBaseResponse;
                initListClass(getserveltclassResponse.classes);
            }
        }, mProgressBar);
        mGetproducttypeRequest = new GetproducttypeRequest(new ApiListenrDefaultImpl()
        {
            @Override
            public void onSuccess(B5MBaseResponse b5mBaseResponse)
            {
                // TODO Auto-generated method stub
                super.onSuccess(b5mBaseResponse);
                getproducttypeResponse = (GetproducttypeResponse) b5mBaseResponse;
                initListType(getproducttypeResponse.productTypes);
            }
        }, mProgressBar);
    }

    protected void initListType(List<ProductType> productTypes)
    {
        // TODO Auto-generated method stub
        int length = productTypes.size();
        for (int i = 0; i < length; i++)
        {
            mListType.add(productTypes.get(i).typename);
        }
        mListType.add(0, "无");
        mArrayAdapterType.notifyDataSetChanged();
    }

    protected void initListClass(List<Class> classes)
    {
        // TODO Auto-generated method stub
        int length = classes.size();
        for (int i = 0; i < length; i++)
        {
            mListClass.add(classes.get(i).title);
        }
        mListClass.add(0, "无");
        mArrayAdapterClass.notifyDataSetChanged();
    }

    @Override
    public void doAfter()
    {
        // TODO Auto-generated method stub
        mGetserveltclassRequest.start();
        mGetproducttypeRequest.start();
    }

    public void submit(View view)
    {
        String area = et_area.getText().toString().trim();
        String keyword = et_keyword.getText().toString().trim();
        String lowestprice = et_lowestprice.getText().toString().trim();
        String highestprice = et_highestprice.getText().toString().trim();
        if (TextUtils.isEmpty(area))
        {
            B5MToaster.showShort(mActivity, "地区不能为空", 0);
            return;
        }
        if (TextUtils.isEmpty(keyword))
        {
            B5MToaster.showShort(mActivity, "旅游产品或服务不能为空", 0);
            return;
        }
        if (!TextUtils.isEmpty(lowestprice) && !TextUtils.isEmpty(highestprice))
        {
            if (Integer.valueOf(highestprice) - Integer.valueOf(lowestprice) < 0)
            {
                B5MToaster.showShort(mActivity, "最高价格不能小于最低价格", 0);
                return;
            }
        }
        Bundle bundle = new Bundle();
        bundle.putString("area", area);
        bundle.putString("skey", keyword);
        if (!TextUtils.isEmpty(typeid))
        {
            bundle.putString("typeid", typeid);
        }
        if (!TextUtils.isEmpty(lowestprice))
        {
            bundle.putString("priceA", lowestprice);
        }
        if (!TextUtils.isEmpty(highestprice))
        {
            bundle.putString("priceB", highestprice);
        }
        bundle.putString("type", SearchAdvancedActivity.class.getName());
        B5MActivityHelper.startActivity(mActivity, SearchResultActivity.class, bundle);
    }
}
